#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <wiiuse/wpad.h>

#include "libwiigui/gui.h"
#include "menu.h"
#include "alacarte.h"
#include "input.h"
#include "filelist.h"
#include "filebrowser.h"

#include <sstream>

#include "homebrewboot/HomebrewFiles.h"

#include "menu.h"

#define DISABLED_ALPHA 80

using namespace std;


int MenuDialPad() {
	int menu = MENU_NONE;

	GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, SOUND_PCM);
	GuiImageData btnOutline(button_png);
	GuiImageData btnOutlineOver(button_over_png);
	GuiImageData btnLargeOutline(button_large_png);
	GuiImageData btnLargeOutlineOver(button_large_over_png);

	// Pad button A
	GuiTrigger trigA;
	trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
	// Pad button Home
	GuiTrigger trigHome;
	trigHome.SetButtonOnlyTrigger(-1, WPAD_BUTTON_HOME | WPAD_CLASSIC_BUTTON_HOME, 0);
	
	
	GuiText exitBtnTxt("Quit", 22, (GXColor) {0, 0, 0, 255});
	GuiImage exitBtnImg(&btnOutline);
	GuiImage exitBtnImgOver(&btnOutlineOver);
	GuiButton exitBtn(btnOutline.GetWidth(), btnOutline.GetHeight());
	exitBtn.SetAlignment(ALIGN_LEFT, ALIGN_BOTTOM);
	exitBtn.SetPosition(100, -35);
	exitBtn.SetLabel(&exitBtnTxt);
	exitBtn.SetImage(&exitBtnImg);
	exitBtn.SetImageOver(&exitBtnImgOver);
	exitBtn.SetSoundOver(&btnSoundOver);
	exitBtn.SetTrigger(&trigA);
	exitBtn.SetTrigger(&trigHome);
	exitBtn.SetEffectGrow();
		
	GuiImageData utilDisabledData(button_util_disabled_png);
	GuiImage utilDisabled(&utilDisabledData);
	utilDisabled.SetAlpha(DISABLED_ALPHA);
	GuiImageData utilData(button_util_png);
	GuiImage util(&utilData);
	
	GuiButton btUtil(64, 64);
	btUtil.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	btUtil.SetPosition(-20, 10);
	btUtil.SetImage(&utilDisabled);
	btUtil.SetImageOver(&util);
	btUtil.SetSoundOver(&btnSoundOver);
	btUtil.SetEffectGrow();
	
	GuiImageData mediaDisabledData(button_media_disabled_png);
	GuiImage mediaDisabled(&mediaDisabledData);
	mediaDisabled.SetAlpha(DISABLED_ALPHA);
	GuiImageData mediaData(button_media_png);
	GuiImage media(&mediaData);
	
	GuiButton btMedia(64, 64);
	btMedia.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	btMedia.SetPosition(-20 - 64, 10);
	btMedia.SetImage(&mediaDisabled);
	btMedia.SetImageOver(&media);
	btMedia.SetSoundOver(&btnSoundOver);
	btMedia.SetEffectGrow();
	
	GuiImageData gamesDisabledData(button_games_disabled_png);
	GuiImage gamesDisabled(&gamesDisabledData);
	gamesDisabled.SetAlpha(DISABLED_ALPHA);
	GuiImageData gamesData(button_games_png);
	GuiImage games(&gamesData);
	
	GuiButton btGames(64, 64);
	btGames.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	btGames.SetPosition(-20 - 64 * 2, 10);
	btGames.SetImage(&gamesDisabled);
	btGames.SetImageOver(&games);
	btGames.SetSoundOver(&btnSoundOver);
	btGames.SetEffectGrow();
	
	GuiImageData emulatorsDisabledData(button_emulators_disabled_png);
	GuiImage emulatorsDisabled(&emulatorsDisabledData);
	emulatorsDisabled.SetAlpha(DISABLED_ALPHA);
	GuiImageData emulatorsData(button_emulators_png);
	GuiImage emulators(&emulatorsData);
	
	GuiButton btEmulators(64, 64);
	btEmulators.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	btEmulators.SetPosition(-20 - 64 * 3, 10);
	btEmulators.SetImage(&emulatorsDisabled);
	btEmulators.SetImageOver(&emulators);
	btEmulators.SetSoundOver(&btnSoundOver);
	btEmulators.SetEffectGrow();
	
	GuiImageData demosDisabledData(button_demos_disabled_png);
	GuiImage demosDisabled(&demosDisabledData);
	demosDisabled.SetAlpha(DISABLED_ALPHA);
	GuiImageData demosData(button_demos_png);
	GuiImage demos(&demosData);
	
	GuiButton btDemos(64, 64);
	btDemos.SetAlignment(ALIGN_RIGHT, ALIGN_TOP);
	btDemos.SetPosition(-20 - 64 * 4, 10);
	btDemos.SetImage(&demosDisabled);
	btDemos.SetImageOver(&demos);
	btDemos.SetSoundOver(&btnSoundOver);
	btDemos.SetEffectGrow();

	HaltGui();
	GuiWindow w(screenwidth, screenheight);

	w.Append(&exitBtn);
	
	w.Append(&btUtil);
	w.Append(&btMedia);
	w.Append(&btGames);
	w.Append(&btEmulators);
	w.Append(&btDemos);
	
	mainWindow->Append(&w);
	
	// The source above is an example of big button.. I'll use this reference
	// to use in the "dial pad" page...
	
	//TODO: Align on center...
	int s = 50;
	int imgW = 194;
	int imgH = 96;
	
	//will be GuiWindow????
	//for (int i = 0; i < 4; i++) { // cells
		GuiImage btImg(&btnLargeOutline);
		GuiImage btImgOver(&btnLargeOutlineOver);
		GuiButton bt(imgW, imgH);
		bt.SetImage(&btImg);
		bt.SetImageOver(&btImgOver);
		
		bt.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
		bt.SetPosition(s, 100);
		bt.SetEffectGrow();
		
		w.Append(&bt);
		
		GuiImage btImg2(&btnLargeOutline);
		GuiImage btImgOver2(&btnLargeOutlineOver);
		GuiButton bt2(imgW, imgH);
		bt2.SetImage(&btImg2);
		bt2.SetImageOver(&btImgOver2);
		
		bt2.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
		bt2.SetPosition((imgW * 1) + s, 100);
		bt2.SetEffectGrow();
		
		w.Append(&bt2);
		
		GuiImage btImg3(&btnLargeOutline);
		GuiImage btImgOver3(&btnLargeOutlineOver);
		GuiButton bt3(imgW, imgH);
		bt3.SetImage(&btImg3);
		bt3.SetImageOver(&btImgOver3);
		
		bt3.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
		bt3.SetPosition((imgW * 2) + s, 100);
		bt3.SetEffectGrow();
		
		w.Append(&bt3);
	//}
	
	
	/*
	 * 
	GuiText menuBtnTxt("Menu", 22, (GXColor) {0, 0, 0, 255});
	menuBtnTxt.SetWrap(true, btnLargeOutline.GetWidth() - 30);
	GuiImage menuBtnImg(&btnLargeOutline);
	GuiImage menuBtnImgOver(&btnLargeOutlineOver);
	GuiButton menuBtn(btnLargeOutline.GetWidth(), btnLargeOutline.GetHeight());
	menuBtn.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
	menuBtn.SetPosition(-125, 250);
	menuBtn.SetLabel(&menuBtnTxt);
	menuBtn.SetImage(&menuBtnImg);
	menuBtn.SetImageOver(&menuBtnImgOver);
	menuBtn.SetSoundOver(&btnSoundOver);
	menuBtn.SetTrigger(&trigA);
	menuBtn.SetEffectGrow();
	*/
	
	
	//Read the XMLs
	//read the homebrew files (meta.xml) information...
	HomebrewFiles HomebrewFiles("SD:/apps/");

	u32 filecount = HomebrewFiles.GetFilecount();
	
	if (!filecount) {
		WindowPrompt("No applications", "No homebrew applications has been found.", "If you say so...", NULL);
    }
	
	
	//Setup Dial Pad buttons...
	
	
	
//
//	std::string s;
//	std::stringstream out;
//	out << filecount;
//	s = out.str();
//
//	string message = "files: " + s + " ....";
//	
//	GuiText tempTxt(message.data(), 28, (GXColor) {0, 0, 0, 255});
//	tempTxt.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
//	tempTxt.SetPosition(50, 100);
//	w.Append(&tempTxt);
	
	

	ResumeGui();

	while (menu == MENU_NONE) {
		usleep(THREAD_SLEEP);
		
//		if (menuBtn.GetState() == STATE_CLICKED) {
//			menu = MENU_SETTINGS_FILE;
		if (exitBtn.GetState() == STATE_CLICKED) {
			int choice = WindowPrompt("Exit Alacarte", "Exit to Homebrew Channel?", "Yes", "No");
			if (choice == 1) {
				menu = MENU_EXIT;
			}
		}
	}

	HaltGui();
	mainWindow->Remove(&w);
	return menu;
}
